
component  output="false" extends="AbsFactory"
{

	public UserFactory function init(){
		
		return this;
	}
	
	public it.millemultimedia.easyBlog.validator.UserValidator function getValidator(){
		
		return new it.millemultimedia.easyBlog.validator.UserValidator();
	}
	
	public it.millemultimedia.easyBlog.model.User function createInstance(){
		
		return new it.millemultimedia.easyBlog.model.User();
	}
	
	public it.millemultimedia.easyBlog.model.User function get( required Query Users, required Query permissions, required Query preferences ){
		
		var obj = createInstance() ;
		var i = 1 ;
		var p = 1 ;
		var c = 1 ;
		
		obj = build( arguments.Users, i, arguments.permissions, p, arguments.preferences, c );
		
		return obj;
		
	}
	
	public it.millemultimedia.easyBlog.model.User[] function list( required Query Users, required Query permissions, required Query preferences, Numeric start=0, Numeric count=0 ){
		
		var list = createObject( 'java','java.util.ArrayList' ).init();
		var obj = "";
		var end = 0 ;
		var i = 1 ;
		var p = 1 ;
		var c = 1 ;
		
		if( arguments.start eq 0 and arguments.count eq 0 ){
		
			arguments.start = 1;
			end = arguments.Users.recordcount;
		
		}else{
		
			if( (arguments.start + arguments.count - 1) GTE arguments.Users.recordcount ){
			
				end =  arguments.Users.recordcount ;
				
			}else{
			
				end= arguments.start + arguments.count - 1 ;
	
			}
		
		}
		
		for( i=arguments.start; i<=end; i++ ){
		
			obj = build( arguments.Users, i, arguments.permissions, p, arguments.preferences, c ); ;
			
			list.add( obj ) ;
		
		}
		
		return list;
		
	}
	
	private it.millemultimedia.easyBlog.model.User function build( required Query Users, required Numeric index, Query permissions, Numeric indexPermissions, Query preferences, Numeric indexPreferences  ){
		
		var obj = createInstance();
		var i = arguments.index;
		
		if( structKeyExists( arguments.Users, 'Iduser' ) ){
		
			obj.setIduser(arguments.Users.Iduser[i]);

		}
		if( structKeyExists( arguments.Users, 'Status' ) ){
		
			obj.setStatus(arguments.Users.Status[i]);

		}
		if( structKeyExists( arguments.Users, 'Editable' ) ){
		
			obj.setEditable(arguments.Users.Editable[i]);

		}
		if( structKeyExists( arguments.Users, 'Username' ) ){
		
			obj.setUsername(arguments.Users.Username[i]);

		}
		if( structKeyExists( arguments.Users, 'Password' ) ){
		
			obj.setPassword(arguments.Users.Password[i]);

		}
		if( structKeyExists( arguments.Users, 'Email' ) ){
		
			obj.setEmail(arguments.Users.Email[i]);

		}
		if( structKeyExists( arguments.Users, 'Uuid' ) ){
		
			obj.setUuid(arguments.Users.Uuid[i]);

		}
		if( structKeyExists( arguments.Users, 'Name' ) ){
		
			obj.setName(arguments.Users.Name[i]);

		}
		if( structKeyExists( arguments.Users, 'Shortdescription' ) ){
		
			obj.setShortdescription(arguments.Users.Shortdescription[i]);

		}
		if( structKeyExists( arguments.Users, 'Description' ) ){
		
			obj.setDescription(arguments.Users.Description[i]);

		}
		if( structKeyExists( arguments.Users, 'IdRole' ) ){
		
			obj.setIdRole(arguments.Users.IdRole[i]);

		}
		
		obj.setPermissions( getPermissions( arguments.permissions, arguments.indexPermissions, arguments.Users.Iduser[i] ) );
		obj.setPreferences( getPreferences( arguments.preferences, arguments.indexPreferences, arguments.Users.Iduser[i] ) );
		
		return obj;
		
	}
	
	private it.millemultimedia.easyBlog.model.Permission[] function getPermissions( required Query permissions, Numeric index, Numeric idUser ){
		
		var qry = arguments.permissions;
		var list = createObject( 'java','java.util.ArrayList' ).init();
		var obj = "";
		var i = arguments.index ;
		
		for( i=i; i<=qry.recordCount; i++ ){
		
			if( arguments.idUser eq qry.Iduser[i] ){
		
				obj = new it.millemultimedia.easyBlog.model.Permission() ;
				
				obj.setId(qry.id[i]);
				obj.setName(qry.Name[i]);
				obj.setDescription(qry.Description[i]);
				
				list.add( obj ) ;
			
			}
		
		}
		
		return list;
		
	}
	
	private it.millemultimedia.easyBlog.model.Preference[] function getPreferences( required Query preferences, Numeric index, Numeric idUser ){
		
		var qry = arguments.preferences;
		var list = createObject( 'java','java.util.ArrayList' ).init();
		var obj = "";
		var i = arguments.index ;
		
		for( i=i; i<=qry.recordCount; i++ ){
		
			if( arguments.idUser eq qry.Iduser[i] ){
		
				obj = new it.millemultimedia.easyBlog.model.Preference() ;
				
				obj.setId(qry.id[i]);
				obj.setName(qry.Name[i]);
				obj.setDescription(qry.Description[i]);
				
				list.add( obj ) ;
			
			}
		
		}
		
		return list;
		
	}

}
